<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2020/4/1
  Time: 11:02
  To change this template use File | Settings | File Templates.
--%>
<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2018/4/7
  Time: 16:45
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <title>新增员工</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1 align="center">
                    基于SSM框架的管理系统：简单实现增、删、改、查。
                </h1>
            </div>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>新增员工</small>
                </h1>
            </div>
        </div>
    </div>
    <form action="" name="userForm">
        姓名：<input type="text" name="name" id="name" oninput="resName()"><span id="sname"></span><br><br><br>
        性别：<input type="text" name="sex" id="sex" oninput="resSex()"><span id="ssex"></span><br><br><br>
        出生日期：<input type="date" name="birthday" id="birthday"><span id="sbirthday"></span><br><br><br>
        籍贯：<input type="text" name="birthplace" id="birthplace" oninput="resBirthplace()"><span id="sbirthplace"></span><br><br><br>
        学历：<input type="text" name="education" id="education" oninput="resEducation()"><span id="seducation"></span><br><br><br>
        地址：<input type="text" name="address" id="address" oninput="resAddress()"><span id="saddress"></span><br><br><br>
        电话：<input type="text" name="tele" id="tele" oninput="resTele()"><span id="stele"></span><br><br><br>
        工龄：<input type="text" name="workage" id="workage" oninput="resWorkage()"><span id="sworkage"></span><br><br><br>
        基本工资：<input type="text" name="salary" id="salary" oninput="resSalary()"><span id="ssalary"></span><br><br><br>

        <input type="button" value="添加" id="btnCommit" disabled="disabled" onclick="addWorker()">

    </form>

    <script type="text/javascript">
        function addWorker() {
            var form = document.forms[0];
            form.action = "<%=basePath %>worker/addWorker";
            form.method = "post";
            form.submit();
        }
        // 0表示无法进行添加,1表示可以添加
        var flag_name = 0;
        var flag_sex = 0;
        var flag_education = 0;
        var flag_birthplace = 0;
        var flag_address = 0;
        var flag_tele = 0;
        var flag_workage = 0;
        var flag_salary = 0;

        // function restraint()
        // {
        //     document.getElementById("btnCommit").disable = !(resName() === 1 && resSex() === 1 && resEducation() === 1);
        //     if ((20 > document.getElementById("name").value.length > 0 ) && (document.getElementById("sex").value === "1" || document.getElementById("sex").value === "0") && (document.getElementById("education").value === "0" || document.getElementById("education").value === "1" || document.getElementById("education").value === "2" || document.getElementById("education").value === "3" || document.getElementById("education").value === "4"))
        //     {
        //         document.getElementById("btnCommit").disabled = false;
        //     }
        // }


        //name 0<表示长度<=20
        function resName() {
            if (document.getElementById("name").value.length === 0 || document.getElementById("name").value.length > 20)
            {
                document.getElementById("sname").innerText = "姓名长度必须大于零或小于20！";
                document.getElementById("btnCommit").disabled = true;
            }
            else
            {
                document.getElementById("sname").innerText = "";
                flag_name = 1;
                document.getElementById("btnCommit").disabled = !(flag_salary ===1 && flag_workage === 1 && flag_tele === 1 && flag_address === 1 && flag_birthplace === 1 && flag_name === 1 && flag_sex === 1 && flag_education === 1);
            }
            return flag_name;
        }

        //sex 0(女) or 1（男）
        function resSex() {
            if (document.getElementById("sex").value === "1" || document.getElementById("sex").value === "0")
            {
                document.getElementById("ssex").innerText = "";
                flag_sex = 1;
                document.getElementById("btnCommit").disabled = !(flag_salary ===1 && flag_workage === 1 && flag_tele === 1 && flag_address === 1 && flag_birthplace === 1 && flag_name === 1 && flag_sex === 1 && flag_education === 1);
            }
            else
            {
                document.getElementById("ssex").innerText = "请用使用0或1表示女或男！";
                document.getElementById("btnCommit").disabled = true;
            }
            return flag_sex;
        }

        //出生日期年四个数字(1900<2004)、月两个数字(1-12)、日(1-31)
        function resBirthday() {

        }

        //学历 0高中1学士2硕士3博士4其它
        function resEducation() {
            if (document.getElementById("education").value === "0" || document.getElementById("education").value === "1" || document.getElementById("education").value === "2" || document.getElementById("education").value === "3" || document.getElementById("education").value === "4")
            {
                document.getElementById("seducation").innerText = "";
                flag_education = 1;
                document.getElementById("btnCommit").disabled = !(flag_salary ===1 && flag_workage === 1 && flag_tele === 1 && flag_address === 1 && flag_birthplace === 1 && flag_name === 1 && flag_sex === 1 && flag_education === 1);
            }
            else
            {
                document.getElementById("seducation").innerText = "请用使用0、1、2、3、4分别表示高中、学士、硕士、博士、其他学位！";
                document.getElementById("btnCommit").disabled = true;
            }
            return flag_education;
        }

        //籍贯 0<表示长度<=10
        function resBirthplace() {
            if ( document.getElementById("birthplace").value.length === 0 || document.getElementById("birthplace").value.length > 20)
            {
                document.getElementById("sbirthplace").innerText = "籍贯长度必须大于零或小于10！";
                document.getElementById("btnCommit").disabled = true;
            }
            else
            {
                document.getElementById("sbirthplace").innerText = "";
                flag_birthplace = 1;
                document.getElementById("btnCommit").disabled = !(flag_salary ===1 && flag_workage === 1 && flag_tele === 1 && flag_address === 1 && flag_birthplace === 1 && flag_name === 1 && flag_sex === 1 && flag_education === 1);
            }
            return flag_birthplace;
        }

        //地址0<表示长度<=30
        function resAddress() {
            if(document.getElementById("address").value.length ===0 || document.getElementById("address").value.length > 30)
            {
                document.getElementById("saddress").innerText = "地址长度必须大于零或小于30！";
                document.getElementById("btnCommit").disabled = true;
            }
            else
            {
                document.getElementById("saddress").innerText = "";
                flag_address = 1;
                document.getElementById("btnCommit").disabled = !(flag_salary ===1 && flag_workage === 1 && flag_tele === 1 && flag_address === 1 && flag_birthplace === 1 && flag_name === 1 && flag_sex === 1 && flag_education === 1);
            }
            return flag_address;
        }

        //电话 0<表示长度<=15，有分机时在后面使用’-’接分机号
        function resTele() {
            var integer1 = /^((\d{8})|(0\d{2,3}-\d{8,9})|(1[34578]\d{9}))$/;
            if(!integer1.test(document.getElementById("tele").value) || document.getElementById("tele").value.length===0)
            {
                document.getElementById("stele").innerText = "长度不正确或电话号无效";
                document.getElementById("btnCommit").disabled = true;
            }
            else
            {
                document.getElementById("stele").innerText = "";
                flag_tele = 1;
                document.getElementById("btnCommit").disabled = !(flag_salary ===1 && flag_workage === 1 && flag_tele === 1 && flag_address === 1 && flag_birthplace === 1 && flag_name === 1 && flag_sex === 1 && flag_education === 1);
            }
            return flag_tele;
        }

        //工龄 0<工龄<50
        function resWorkage() {
            var a = document.getElementById("workage").value;
            var workage = parseInt(a);
            if ( workage <= 0 || workage >= 50)
            {
                document.getElementById("sworkage").innerText = "工龄必须大于零小于50！";
                document.getElementById("btnCommit").disabled = true;
            }
            else
            {
                document.getElementById("sworkage").innerText = "";
                flag_workage = 1;
                document.getElementById("btnCommit").disabled = !(flag_salary ===1 && flag_workage === 1 && flag_tele === 1 && flag_address === 1 && flag_birthplace === 1 && flag_name === 1 && flag_sex === 1 && flag_education === 1);
            }
            return flag_workage;
        }

        //基本工资 0<工资
        function resSalary() {
            var a = document.getElementById("salary").value;
            var salary = parseFloat(a);
            if (salary <= 0)
            {
                document.getElementById("ssalary").innerText = "工资必须大于零！";
                document.getElementById("btnCommit").disabled = true;
            }
            else
            {
                document.getElementById("ssalary").innerText = "";
                flag_salary = 1;
                document.getElementById("btnCommit").disabled = !(flag_salary ===1 && flag_workage === 1 && flag_tele === 1 && flag_address === 1 && flag_birthplace === 1 && flag_name === 1 && flag_sex === 1 && flag_education === 1);
            }
        }

    </script>
</div>
